package com.ihr360.payroll.calc;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.ImmutableList;
import com.ihr360.common.DateRange;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.lang.DateUtils;
import com.ihr360.commons.model.meta.table.DynamicCell;
import com.ihr360.payroll.type.StaffRefStatus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 对于分段的人，一个人的计算结果要考虑分段信息和汇总信息的保存 SalaryProfileItemsWrapper 目前是满足不了修改的，需要
 * 重新设计
 */
@JsonIgnoreProperties(ignoreUnknown = true)

public class StaffSalaryCalcPeriod implements Serializable {
    private String companyId;

    private Long termId;

    private String staffId;

    private String staffName;

    private String mobileNo;

    private String departmentName;

    private Long departmentId;

    private int year;
    private int month;
    /**
     * 计算开始日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8" )
    private Date startOn;
    /**
     * 计算结束日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8" )
    private Date endOn;

    private Map<String, DynamicCell> summarizedData;

    private List<StaffSalaryCalcPeriodTerm> phases;

    private StaffRefStatus sihfStatus;
    private StaffRefStatus attendanceStatus;

    public StaffSalaryCalcPeriod() {
    }

    public StaffSalaryCalcPeriod(String staffId, String staffName) {
        this.staffId = staffId;
        this.staffName = staffName;
    }

    public StaffSalaryCalcPeriod(StaffSalaryCalcPeriodTerm term) {
        this.phases = ImmutableList.of(term);
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public Long getTermId() {
        return termId;
    }

    public void setTermId(Long termId) {
        this.termId = termId;
    }

    public void setStaffId(String staffId) {
        this.staffId = staffId;
    }

    public StaffSalaryCalcPeriod(List<StaffSalaryCalcPeriodTerm> terms) {
        this.phases = terms;
    }

    public List<StaffSalaryCalcPeriodTerm> getPhases() {
        return phases;
    }

    public int getPhaseCount() {
        if (CollectionUtils.isEmpty(phases)) {
            return 0;
        }
        return phases.size();
    }

    public void setStaffName(String staffName) {
        this.staffName = staffName;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public Date getStartOn() {
        return startOn;
    }

    public void setStartOn(Date startOn) {
        this.startOn = startOn;
    }

    public Date getEndOn() {
        return endOn;
    }

    public void setEndOn(Date endOn) {
        this.endOn = endOn;
    }

    public StaffRefStatus getSihfStatus() {
        return sihfStatus;
    }

    public void setSihfStatus(StaffRefStatus sihfStatus) {
        this.sihfStatus = sihfStatus;
    }

    public StaffRefStatus getAttendanceStatus() {
        return attendanceStatus;
    }

    public void setAttendanceStatus(StaffRefStatus attendanceStatus) {
        this.attendanceStatus = attendanceStatus;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    @JsonIgnore
    public List<StaffSalaryCalcPeriodTerm> getPhasesInRange(DateRange range) {
        if (CollectionUtils.isEmpty(phases)) {
            return Collections.emptyList();
        }
        List<StaffSalaryCalcPeriodTerm> inRangedTerms = new ArrayList<>(3);
        for (StaffSalaryCalcPeriodTerm term : phases) {
            if (DateUtils.isBetween(term.getStartOn(), range.getStart(), range.getEnd())
                    && DateUtils.isBetween(term.getEndOn(), range.getStart(), range.getEnd())) {
                inRangedTerms.add(term);
            }
        }
        return inRangedTerms;
    }

    public String getStaffId() {
        return staffId;
    }

    public String getStaffName() {
        return staffName;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public void setPhases(List<StaffSalaryCalcPeriodTerm> phases) {
        this.phases = phases;
    }

    public boolean hasPhase() {
        return CollectionUtils.isNotEmpty(phases);
    }

    /**
     * 没有分段数据和只有一段数据等同于任务是没有分段的
     *
     * @return
     */
    public boolean isSinglePhase() {
        if (!hasPhase()) {
            return true;
        }
        return CollectionUtils.isNotEmpty(phases) && phases.size() == 1;
    }

    public Map<String, DynamicCell> getSummarizedData() {
        return summarizedData;
    }


    public void setSummarizedData(Map<String, DynamicCell> summarizedData) {
        this.summarizedData = summarizedData;
    }

    public void addSummarizedItem(String field, String value) {
        if (summarizedData == null) {
            this.summarizedData = new HashMap<>();
        }
        if (value != null) {
            this.summarizedData.put(field, new DynamicCell(value));
        } else {
            this.summarizedData.remove(field);
        }
    }

    public void addVoidSummarizedItem(String field) {
        if (summarizedData == null) {
            this.summarizedData = new HashMap<>();
        }
        this.summarizedData.remove(field);
    }

    public void addSummarizedItem(String field, DynamicCell value) {
        if (summarizedData == null) {
            this.summarizedData = new HashMap<>();
        }
        if (value != null) {
            if (value.getCellValue() == null) {
                this.summarizedData.remove(field);
                return;
            }
            this.summarizedData.put(field, value);
            return;
        }
        this.summarizedData.remove(field);
    }

    public void clearSummarizedData() {
        if (this.summarizedData != null) {
            this.summarizedData.clear();
        }
        this.summarizedData = null;
    }

    public void clearPhasedData() {
        if (phases != null) {
            phases.clear();
            phases = null;
        }
    }

    public List<StaffSalaryCalcPeriodTerm> sortedStaffSalaryCalcPeriodTerms() {
        List<StaffSalaryCalcPeriodTerm> phases = getPhases();
        if (CollectionUtils.isEmpty(phases)) {
            return null;
        }
        phases = phases.stream().sorted((p1, p2) -> p2.getStartOn().compareTo(p1.getStartOn())).collect(Collectors.toList());
        return phases;
    }
}